package com.edt.ichannel.api.service.impl;

import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.dao.OrderCreditKdDao;
import com.edt.ichannel.api.entiy.local.OrderCreditKd;
import com.edt.ichannel.api.service.OrderCreditKdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * OrderCreditKd服务接口实现类
 *
 * @author
 * @date 2018-09-27
 */
@Service
@Slf4j
public class OrderCreditKdServiceImpl implements OrderCreditKdService {

    @Resource
    private OrderCreditKdDao ordercreditkdDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrderCreditKdList(List<OrderCreditKd> orderCreditKds) {
        if (orderCreditKds != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < orderCreditKds.size();){
                if(lastIndex > orderCreditKds.size()){
                    lastIndex = orderCreditKds.size();
                    ordercreditkdDao.saveOrderCreditKdList(orderCreditKds.subList(index,lastIndex));
                    break;
                }else{
                    ordercreditkdDao.saveOrderCreditKdList(orderCreditKds.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            log.info("宽带list 长度："+orderCreditKds.size());
        }else{
            log.info("宽带list为空");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult saveOrderCreditKd(OrderCreditKd ordercreditkd) {
        ActionResult actionResult = new ActionResult();

        ordercreditkdDao.saveOrderCreditKd(ordercreditkd);
        actionResult.setMessage("添加成功。");
        actionResult.setSuccess(CommonConstant.ACTIONRESULT_TRUE);

        return actionResult;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult updateOrderCreditKd(OrderCreditKd ordercreditkd) {
        ActionResult actionResult = new ActionResult();

        ordercreditkdDao.updateOrderCreditKd(ordercreditkd);
        actionResult.setMessage("修改成功。");
        actionResult.setSuccess(CommonConstant.ACTIONRESULT_TRUE);

        return actionResult;
    }

    @Override
    public OrderCreditKd selectOrderCriditKdByPhoneAndUserId(String phoneNumber, String userId) {
        return ordercreditkdDao.selectOrderCriditKdByPhoneAndUserId(phoneNumber,userId);
    }

    @Override
    public void moveOrderCreditKdToOrderCredit() {
        ordercreditkdDao.moveOrderCreditKdToOrderCredit();
    }

    @Override
    public List<OrderCreditKd> selectNeedPutOut() {
        return ordercreditkdDao.selectNeedPutOut();
    }

    @Override
    public OrderCreditKd selectOrderCreditKdByOrderCode(String orderCode) {
        return ordercreditkdDao.selectOrderCreditKdByOrderCode(orderCode);
    }

    @Override
    public void saveOrUpdateOrderCreditKd(OrderCreditKd orderCreditKd) {
        ordercreditkdDao.saveOrUpdateOrderCreditKd(orderCreditKd);
    }

    @Override
    public void moveOrderCreditKdToOrderCreditByOrderCode(String orderCode) {
        ordercreditkdDao.moveOrderCreditKdToOrderCreditByOrderCode(orderCode);
    }

    @Override
    public void overFlowMonthCommission(String id) {
        ordercreditkdDao.overFlowMonthCommission(id);
    }

    @Override
    public void addCommissionMoney(String id, Double commissionMoney) {
        ordercreditkdDao.addCommissionMoney(id,commissionMoney);
    }

    @Override
    public void appendRemark(String id, String s) {
        ordercreditkdDao.appendRemark(id, s);
    }


}